Q4: Explain
Looping Concept. Write Syntax of all three looping statements.
Loops:
In real life, there are situations
when there is need to perform a set of task repeatedly till some condition is
met. Such as – sending email to all employees, deleting all files, printing
1000 pages of a document. All of these tasks are performed in loop.
There
are three kind of looping in C Language:
a. While Loop,
b. Do While,
c. For Loop.
For Loop
Parts of for loop
Any repetition contains two
important part - What to repeat and number of repetition?
Variable-initialization, condition and variable-update define number of
repetition and body of loop defines what to repeat.
·
Variable-initialization
contain loop counter variable initialization statements. It define starting
point of the repetition (where to start loop).
·
Condition contain
Boolean expressions and works like if...else. If boolean expression is true,
then execute body of loop otherwise terminate the loop.
·
Body of loop
specifies what to repeat. It contain set of statements to repeat.
·
Variable-update
contains loop counter update (increment/decrement) statements.
How for loop works?
·
Initially variable-initialization
block receive program control. It is non-repeatable part and executed only once
throughout the execution of for loop. After initialization program control is
transferred to loop condition.
·
The loop condition
block evaluates all Boolean expression and determines loop should continue or
not. If loop conditions are met, then it transfers program control to body of
loop otherwise terminate the loop. In C we specify a boolean expression using relational and logical
operator.
·
Body of loop executes
a set of statements. After executing all statements it transfer program control
to variable-update block.
·
Variable-update block updates loop counter variable and transfer program
control again back to condition block of loop.
Step
2 to 4 is repeated until condition is met.
Control Flow of for loop
#include <stdio.h> void main() { int count; for(count=1;
count<=10; count++) { printf("%d
", count); } }

Syntax of for loop
for(variable-initialization
; condition ; variable-update)
{
// Body of for loop
While Loop
for
loop is easy to implement if you specifically know start and end position of
the loop counter. However, things in the real life are not so simple. You may
come across situation where you only know when to terminate the loop. For
example – reading instructions from user until terminated manually, waiting for
client connection until connected or cancelled, reconnecting to the server
until connected.
while
loop is an entry controlled looping construct. We use while loop to repeat set
of statements when number of iterations are not known prior to its execution.
It provides flexibility to define loop without initialization and update parts
(present in for loop).
Syntax of while loop while(condition) { // Body of
while loop }
Control-Flow
#include <stdio.h> void main() { int n = 1; /* Loop
condition */ while(n <=
5) { /* Body
of loop */
printf("%d ", n); /* Update
loop counter variable */ n = n +
1; } }

Do-while Loop
·
C programming supports
three types of looping statements for loop, while loop and do...while loop.
Among three do...while loop is most distinct loop compared to others.
·
do...while is an exit
controlled looping statement.
·
do...while loop is
used, when there is a need to check condition after execution of loop body.
·
do...while loop in
any case executes minimum once.
·
Looping statements
whose condition is checked after execution of its loop body is called as Exit
controlled loop
·
For example -
consider a program to validate user input and run in loop until user feeds
valid input. In this case the input statement should run minimum once and
should repeat in loop until user provides valid input.
Flow Control
Syntax of do...while loop do { // Body of do
while loop } while (condition);

How do...while loop works?
do...while loop works in two
step.
1. Initially program control transfers to body of loop. It
executes all statements inside loop body and transfers control to loop
condition.
2. Loop condition contains set of relational and logical
expressions. If conditional expression evaluates 1 (true) then loop repeats
again otherwise if conditional expression evaluates 0 (false) loop terminates.
The above two steps are
repeated until loop condition is met.
Example:
#include <stdio.h>
void main()
{
/* Loop counter variable declaration */
int n=1`;
do
{
/* Body of loop */
printf("%d ", n);
/* Update loop counter variable */
N = n + 1;
} while(n <= 20); /* Loop condition */
}